package com.shujia;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.ZParams;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.Set;

public class Demo07SortedSet {
    Jedis jedis;

    @Before
    // 在@Test注解方法之前会运行一次
    public void init() {
        jedis = new Jedis("master", 6379);
    }

    @Test
    public void zAdd() {
        HashMap<String, Double> sSet = new HashMap<>();
        sSet.put("西瓜", 2.2);
        sSet.put("香蕉", 6D);
        sSet.put("芒果", 10.0);
        jedis.zadd("ss1", sSet);
    }

    @Test
    public void zRange() {
        for (String ss1 : jedis.zrange("ss1", 0, 1)) {
            System.out.println(ss1);
        }
    }

    @Test
    public void sSetData() {
        HashMap<String, Double> jan = new HashMap<>();
        jan.put("西瓜", 37.8);
        jan.put("香蕉", 22.5);
        jan.put("甘蔗", 40d);
        jedis.zadd("jan", jan);

        HashMap<String, Double> feb = new HashMap<>();
        feb.put("草莓", 13.6);
        feb.put("香蕉", 42.5);
        feb.put("甘蔗", 42d);
        jedis.zadd("feb", feb);
    }

    @Test
    public void zSetUnion() {
        Set<Tuple> tuples = jedis.zunionWithScores(new ZParams().aggregate(ZParams.Aggregate.SUM), "jan", "feb");
        for (Tuple tuple : tuples) {
            System.out.println(tuple);
        }

    }


    @After
    // 在@Test注解方法之后会运行一次
    public void close() {
        jedis.close();
    }
}
